package com.ihr360.payroll.controller.staffweb;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.request.Ihr360SearchBody;
import com.ihr360.payroll.entity.salary.HistoryPayPeriodEntity;
import com.ihr360.payroll.entity.task.PayPeriodEntity;
import com.ihr360.payroll.model.dto.salary.report.HistoryPayPeriod;
import com.ihr360.payroll.model.response.salaryreport.SalaryReportResponse;
import com.ihr360.payroll.model.vo.SalaryDataEntity;
import com.ihr360.payroll.model.vo.SalaryFieldEntity;
import com.ihr360.payroll.request.salaryReport.SalaryReportRequest;
import com.ihr360.payroll.service.payperiod.StaffPayPeriodTermSalaryService;
import com.ihr360.payroll.service.salaryreport.HistoryPayPeriodService;
import com.ihr360.payroll.service.salaryreport.HistorySalaryPlanService;
import com.ihr360.payroll.service.salarytask.SalaryTaskService;
import com.ihr360.shared.basicstaffinfo.dto.SyncedBasicStaffInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * staff端调用接口Controller
 */
@Api(value = "staff端调用接口Controller")
@RestController
@RequestMapping("/salary/staffWeb")
public class PayrollStaffWebController {
    @Autowired
    private SalaryTaskService salaryTaskService;

    @Autowired
    private HistoryPayPeriodService historyPayPeriodService;

    @Autowired
    private HistorySalaryPlanService historySalaryPlanService;

    @Autowired
    private StaffPayPeriodTermSalaryService staffPayPeriodTermSalaryService;


    @ApiOperation(value = "获取账期信息")
    @GetMapping(value = "/periods/{id}")
    public PayPeriodEntity getOne(@PathVariable(name = "id") Long payPeriodId) {
        return salaryTaskService.getPayPeriod(payPeriodId);
    }

    @ApiOperation(value = "获取账期表头信息")
    @GetMapping(value = "/periods/header/{id}")
    public Map<String, SalaryFieldEntity> getPayPeriodStaffHeaderMap(@PathVariable(name = "id") Long payPeriodId) {
        return salaryTaskService.getPayPeriodHeaderMap(payPeriodId);
    }

    @ApiOperation(value = "获取账期数据")
    @PostMapping(value = "/periods/data/{id}")
    public Page<SalaryDataEntity> getPayPeriodStaffDataCells(@RequestParam String companyId,
                                                             @PathVariable(name = "id") Long payPeriodId,
                                                             @RequestBody Ihr360SearchBody<SyncedBasicStaffInfo> searchBody) {
        return salaryTaskService.getPayPeriodStaffDataCells(companyId, payPeriodId, searchBody);
    }

    /**
     * 审批查询薪资报表数据
     */
    @ApiOperation(value = "/salary/staffWeb/approval/staffSalary", notes = "审批查询薪资报表数据")
    @PostMapping(value = "/approval/staffSalary", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public SalaryReportResponse queryApprovalStaffSalary(AuthUserInfo authUserInfo, @RequestBody SalaryReportRequest reportRequest, @RequestParam Long historyPayPeriodId) {
        return staffPayPeriodTermSalaryService.queryApprovalStaffSalary(authUserInfo.getCompanyId(), reportRequest, historyPayPeriodId);
    }

    /**
     * 审批查询薪资报表表头
     */
    @ApiOperation(value = "/salary/staffWeb/approval/staffSalaryHead", notes = "审批查询薪资报表表头")
    @GetMapping(value = "/approval/staffSalaryHead", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public SalaryReportResponse queryApprovalStaffSalaryHead(AuthUserInfo authUserInfo, @RequestParam Long historyPayPeriodId) {
        //获取表头信息
        SalaryReportResponse reportResponse = new SalaryReportResponse();
        reportResponse.setSalaryHeader(historySalaryPlanService.queryApprovalStaffSalaryHead(authUserInfo.getCompanyId(), historyPayPeriodId));
        return reportResponse;
    }

    /**
     * 审批查询历史账期数据
     */
    @ApiOperation(value = "/salary/staffWeb/approval/historyPayPeriod", notes = "审批查询历史账期数据")
    @GetMapping(value = "/approval/historyPayPeriod", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HistoryPayPeriodEntity queryApprovalHistoryPeriod(AuthUserInfo authUserInfo, @RequestParam Long historyPayPeriodId) {
        HistoryPayPeriodEntity entity = new HistoryPayPeriodEntity();
        HistoryPayPeriod historyPayPeriod = historyPayPeriodService.getOne(historyPayPeriodId);
        if (historyPayPeriod == null) {
            throw new Ihr360Exception(null, "该历史账期不存在");
        }
        BeanUtils.copyProperties(historyPayPeriod, entity);
        return entity;
    }

}